
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="Python">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>notebook &#8212; deepaugment 0.2.0 documentation</title>
    <link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
   
  <link rel="stylesheet" href="../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head><body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          

          <div class="body" role="main">
            
  <h1>Source code for notebook</h1><div class="highlight"><pre>
<span></span><span class="c1"># (C) 2019 Baris Ozmen &lt;hbaristr@gmail.com&gt;</span>

<span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>


<div class="viewcode-block" id="get_folder_path"><a class="viewcode-back" href="../notebook.html#notebook.get_folder_path">[docs]</a><span class="k">def</span> <span class="nf">get_folder_path</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
    <span class="n">last</span> <span class="o">=</span> <span class="n">path</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s2">&quot;/&quot;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">path</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">last</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">)</span></div>


<div class="viewcode-block" id="Notebook"><a class="viewcode-back" href="../notebook.html#notebook.Notebook">[docs]</a><span class="k">class</span> <span class="nc">Notebook</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">config</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">store_path</span> <span class="o">=</span> <span class="n">config</span><span class="p">[</span><span class="s2">&quot;notebook_path&quot;</span><span class="p">]</span>

<div class="viewcode-block" id="Notebook.record"><a class="viewcode-back" href="../notebook.html#notebook.Notebook.record">[docs]</a>    <span class="k">def</span> <span class="nf">record</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">trial_no</span><span class="p">,</span> <span class="n">trial_hyperparams</span><span class="p">,</span> <span class="n">sample_no</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">history</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Records one complete training of child model</span>

<span class="sd">        Args:</span>
<span class="sd">            trial_no (int): no of trial (iteration) of training</span>
<span class="sd">            trial_hyperparams (list) : list of data augmentation hyperparameters used for training</span>
<span class="sd">            sample_no (int): sample no among training with same hyperparameters</span>
<span class="sd">            reward (float): reward is basically last n validation accuracy before overfitting</span>
<span class="sd">            history (dict): history returned by keras.model.fit()</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">new_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">history</span><span class="p">)</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;trial_no&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial_no</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;aug1_type&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial_hyperparams</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;aug1_magnitude&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial_hyperparams</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;aug2_type&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial_hyperparams</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;aug2_magnitude&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial_hyperparams</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;portion&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">trial_hyperparams</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;sample_no&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">sample_no</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">reward</span>
        <span class="n">new_df</span> <span class="o">=</span> <span class="n">new_df</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>  <span class="c1"># round all float values to 3 decimals after point</span>
        <span class="n">new_df</span><span class="p">[</span><span class="s2">&quot;epoch&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_df</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">df</span><span class="p">,</span> <span class="n">new_df</span><span class="p">])</span></div>

<div class="viewcode-block" id="Notebook.save"><a class="viewcode-back" href="../notebook.html#notebook.Notebook.save">[docs]</a>    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">store_path</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span></div>

<div class="viewcode-block" id="Notebook.add_records_from"><a class="viewcode-back" href="../notebook.html#notebook.Notebook.add_records_from">[docs]</a>    <span class="k">def</span> <span class="nf">add_records_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">notebook_path</span><span class="p">):</span>
        <span class="n">notebook_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">notebook_path</span><span class="p">,</span> <span class="n">comment</span><span class="o">=</span><span class="s2">&quot;#&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">concat</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">df</span><span class="p">,</span> <span class="n">notebook_df</span><span class="p">])</span></div>

<div class="viewcode-block" id="Notebook.get_top_policies"><a class="viewcode-back" href="../notebook.html#notebook.Notebook.get_top_policies">[docs]</a>    <span class="k">def</span> <span class="nf">get_top_policies</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Prints and returns top-k policies</span>

<span class="sd">        Policies are ordered by their expected accuracy increas</span>
<span class="sd">        Args:</span>
<span class="sd">            k (int) top-k</span>
<span class="sd">        Returns</span>
<span class="sd">            pandas.DataFrame: top-k policies as dataframe</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">trial_avg_val_acc_df</span> <span class="o">=</span> <span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">([</span><span class="s2">&quot;trial_no&quot;</span><span class="p">,</span> <span class="s2">&quot;sample_no&quot;</span><span class="p">])</span>
            <span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s2">&quot;trial_no&quot;</span><span class="p">)</span>
            <span class="o">.</span><span class="n">mean</span><span class="p">()[</span><span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">]</span>
            <span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
        <span class="p">)[[</span><span class="s2">&quot;trial_no&quot;</span><span class="p">,</span> <span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">]]</span>

        <span class="n">x_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">df</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">]),</span>
            <span class="n">trial_avg_val_acc_df</span><span class="p">,</span>
            <span class="n">on</span><span class="o">=</span><span class="s2">&quot;trial_no&quot;</span><span class="p">,</span>
            <span class="n">how</span><span class="o">=</span><span class="s2">&quot;left&quot;</span><span class="p">,</span>
        <span class="p">)</span>

        <span class="n">x_df</span> <span class="o">=</span> <span class="n">x_df</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">,</span> <span class="n">ascending</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>

        <span class="n">baseline_val_acc</span> <span class="o">=</span> <span class="n">x_df</span><span class="p">[</span><span class="n">x_df</span><span class="p">[</span><span class="s2">&quot;portion&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="mf">0.0</span><span class="p">][</span><span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="n">x_df</span><span class="p">[</span><span class="s2">&quot;expected_accuracy_increase&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span>
            <span class="n">x_df</span><span class="p">[</span><span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">]</span> <span class="o">-</span> <span class="n">baseline_val_acc</span>
        <span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">top_df</span> <span class="o">=</span> <span class="n">x_df</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">([</span><span class="s2">&quot;trial_no&quot;</span><span class="p">])</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span>
            <span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">,</span> <span class="n">ascending</span><span class="o">=</span><span class="kc">False</span>
        <span class="p">)[:</span><span class="n">k</span><span class="p">]</span>

        <span class="n">SELECT</span> <span class="o">=</span> <span class="p">[</span>
            <span class="s2">&quot;trial_no&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug1_type&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug1_magnitude&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug2_type&quot;</span><span class="p">,</span>
            <span class="s2">&quot;aug2_magnitude&quot;</span><span class="p">,</span>
            <span class="s2">&quot;portion&quot;</span><span class="p">,</span>
            <span class="s2">&quot;mean_late_val_acc&quot;</span><span class="p">,</span>
            <span class="s2">&quot;expected_accuracy_increase&quot;</span><span class="p">,</span>
        <span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">top_df</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">top_df</span><span class="p">[</span><span class="n">SELECT</span><span class="p">]</span>

        <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;top-</span><span class="si">{k}</span><span class="s2"> policies:&quot;</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">top_df</span><span class="p">)</span>


        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">top_df</span></div>

<div class="viewcode-block" id="Notebook.output_top_policies"><a class="viewcode-back" href="../notebook.html#notebook.Notebook.output_top_policies">[docs]</a>    <span class="k">def</span> <span class="nf">output_top_policies</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">k</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">top_df</span><span class="p">)</span>
        <span class="n">out_path</span> <span class="o">=</span> <span class="n">get_folder_path</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">store_path</span><span class="p">)</span> <span class="o">+</span> <span class="n">f</span><span class="s2">&quot;top</span><span class="si">{k}</span><span class="s2">_policies.csv&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">top_df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="n">out_path</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="nb">print</span><span class="p">(</span><span class="n">f</span><span class="s2">&quot;Top policies are saved to </span><span class="si">{out_path}</span><span class="s2">&quot;</span><span class="p">)</span></div></div>
</pre></div>

          </div>
          
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">deepaugment</a></h1>








<h3>Navigation</h3>

<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../index.html">Documentation overview</a><ul>
  <li><a href="index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>








        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2019, Baris Ozmen.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.3</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
      
    </div>

    

    
  </body>
</html>